package com.smart.campus.web.controller.school;

import java.util.List;

import com.smart.campus.framework.util.ShiroUtils;
import com.smart.campus.school.domain.ExamAnswer;
import com.smart.campus.school.domain.query.ExamQuestionQuery;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.smart.campus.common.annotation.Log;
import com.smart.campus.common.enums.BusinessType;
import com.smart.campus.school.domain.ExamQuestion;
import com.smart.campus.school.service.IExamQuestionService;
import com.smart.campus.common.core.controller.BaseController;
import com.smart.campus.common.core.domain.AjaxResult;
import com.smart.campus.common.utils.poi.ExcelUtil;
import com.smart.campus.common.core.page.TableDataInfo;

/**
 * 试题Controller
 * 
 * @author Zhihe.Li
 * @date 2020-05-20
 */
@Controller
@RequestMapping("/school/question")
public class ExamQuestionController extends BaseController
{
    private String prefix = "school/question";

    @Autowired
    private IExamQuestionService examQuestionService;

    @RequiresPermissions("school:question:view")
    @GetMapping()
    public String question()
    {
        return prefix + "/question";
    }

    /**
     * 查询试题列表
     */
    @RequiresPermissions("school:question:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExamQuestion examQuestion)
    {
        startPage();
        List<ExamQuestion> list = examQuestionService.selectExamQuestionList(examQuestion);
        return getDataTable(list);
    }

    /**
     * 导出试题列表
     */
    @RequiresPermissions("school:question:export")
    @Log(title = "试题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExamQuestion examQuestion)
    {
        List<ExamQuestion> list = examQuestionService.selectExamQuestionList(examQuestion);
        ExcelUtil<ExamQuestion> util = new ExcelUtil<ExamQuestion>(ExamQuestion.class);
        return util.exportExcel(list, "question");
    }

    /**
     * 新增试题
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存试题
     */
    @RequiresPermissions("school:question:add")
    @Log(title = "试题", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExamQuestion examQuestion)
    {
        examQuestion.setCreatBy(ShiroUtils.getSysUser().getCreateBy());
        return toAjax(examQuestionService.insertExamQuestion(examQuestion));
    }

    /**
     * 修改试题
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ExamQuestion examQuestion = examQuestionService.selectExamQuestionById(id);
        mmap.put("examQuestion", examQuestion);
        return prefix + "/edit";
    }

    /**
     * 修改保存试题
     */
    @RequiresPermissions("school:question:edit")
    @Log(title = "试题", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExamQuestion examQuestion)
    {
        return toAjax(examQuestionService.updateExamQuestion(examQuestion));
    }

    /**
     * 删除试题
     */
    @RequiresPermissions("school:question:remove")
    @Log(title = "试题", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(examQuestionService.deleteExamQuestionByIds(ids));
    }


    /**
     * 修改试题
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        ExamQuestion examQuestion = examQuestionService.selectExamQuestionById(id);
        mmap.put("examQuestion", examQuestion);
        return prefix + "/detail";
    }

    @PostMapping("/getQuestionListByCondition")
    @ResponseBody
    public TableDataInfo getQuestionListByCondition(ExamQuestionQuery examQuestionQuery){

        ExamQuestion examQuestion = new ExamQuestion();
        examQuestion.setGradeId(examQuestionQuery.getGradeId());
        examQuestion.setSubject(examQuestionQuery.getSubject());
        examQuestion.setType(examQuestionQuery.getType());
        examQuestion.setParams(examQuestionQuery.getParams());
        examQuestion.setStatus(1);
        startPage();
        List<ExamQuestion> list = examQuestionService.selectExamQuestionList(examQuestion);
        return getDataTable(list);
    }

    /**
     * 报表
     */
    @GetMapping("/report/{id}")
    public String report(@PathVariable("id") Long id, ModelMap mmap)
    {
        ExamQuestion examQuestion = examQuestionService.selectExamQuestionById(id);
        mmap.put("examQuestion", examQuestion);
        return prefix + "/report";
    }
}
